﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Rej;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Rej
{
    public class rej_tblCust_BranchDAL:Irej_tblCust_Branch
    {
        HKEA_CMSEntities model;
        rej_tblCust_Branch branch;

        public rej_tblCust_BranchDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static rej_tblCust_BranchDAL GetInstance()
        {
            return new rej_tblCust_BranchDAL();
        }

        public int InsertENT(rej_tblCust_Branch ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.rej_tblCust_Branch.AddObject(ent);
            return model.SaveChanges();
        }

        public List<rej_tblCust_Branch> GetListByENT(rej_tblCust_Branch ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.rej_tblCust_Branch.Where(a => a.CustBranchUID > 0);
            if (ent.CustBranchUID > 0)
                result = result.Where(a => a.CustBranchUID == ent.CustBranchUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<rej_tblCust_Branch> GetListByENT(rej_tblCust_Branch ent)
        {
            var result = model.rej_tblCust_Branch.Where(a => a.CustBranchUID > 0);
            if (ent.CustBranchUID > 0)
                result = result.Where(a => a.CustBranchUID == ent.CustBranchUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(rej_tblCust_Branch ent)
        {
            var result = model.rej_tblCust_Branch.Where(a => a.CustBranchUID > 0);
            if (ent.CustBranchUID > 0)
                result = result.Where(a => a.CustBranchUID == ent.CustBranchUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(rej_tblCust_Branch ent)
        {
            branch = model.rej_tblCust_Branch.Where(a=>a.CustBranchUID==ent.CustBranchUID).FirstOrDefault();
            if (branch != null)
            {
                branch.AddressUID = ent.AddressUID;
                branch.BranchName_Chi = ent.BranchName_Chi;
                branch.BranchName_Eng = ent.BranchName_Eng;
                branch.ContactInfoUID = ent.ContactInfoUID;
                branch.CustomerUID = ent.CustomerUID;
                branch.IsHQ = ent.IsHQ;
                branch.NOfEmp = ent.NOfEmp;
                branch.RejectedBy = ent.RejectedBy;
                branch.RejectedDate = ent.RejectedDate;
                branch.WebSite = ent.WebSite;
                branch.Deleted = ent.Deleted;
                branch.LastModifyBy = ent.LastModifyBy;
                branch.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(rej_tblCust_Branch ent)
        {
            var temp = model.rej_tblCust_Branch.Where(a => a.CustBranchUID > 0);
            if (ent.CustBranchUID > 0)
                temp = temp.Where(a=>a.CustBranchUID==ent.CustBranchUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.rej_tblCust_Branch.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
